home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / xpaint-2.1.1 / rw / readWriteXPM.c < prev    next >
C/C++ Source or Header  |  1995-05-03  |  4KB  |  150 lines

  1. /* +-------------------------------------------------------------------+ */
  2. /* | Copyright 1993, David Koblas (koblas@netcom.com)                  | */
  3. /* |                                                                   | */
  4. /* | Permission to use, copy, modify, and to distribute this software  | */
  5. /* | and its documentation for any purpose is hereby granted without   | */
  6. /* | fee, provided that the above copyright notice appear in all       | */
  7. /* | copies and that both that copyright notice and this permission    | */
  8. /* | notice appear in supporting documentation.  There is no           | */
  9. /* | representations about the suitability of this software for        | */
  10. /* | any purpose.  this software is provided "as is" without express   | */
  11. /* | or implied warranty.                                              | */
  12. /* |                                                                   | */
  13. /* +-------------------------------------------------------------------+ */
  14.  
  15. #include <stdio.h>
  16. #include <X11/Intrinsic.h>
  17. #include "xpaint.h"
  18. #include "image.h"
  19. #include "xpm.h"
  20.  
  21. int WriteXPM(char *file, Image *image)
  22. {
  23.     XpmAttributes    attr;
  24.  
  25.     attr.valuemask = XpmColormap;
  26.     attr.colormap  = (Colormap)image->sourceColormap;
  27.  
  28.     return XpmWriteFileFromPixmap(Global.display, file,
  29.             image->sourcePixmap, image->sourceMask, &attr) != XpmSuccess;
  30. }
  31.  
  32. int TestXPM(char *file)
  33. {
  34.     FILE    *fd = fopen(file, "r");
  35.     char    buf[40];
  36.     int    i, n, ret = 0;
  37.     int    cstart = False;
  38.  
  39.     if (fd == NULL)
  40.         return 0;
  41.  
  42.     n = fread(buf, sizeof(char), sizeof(buf), fd);
  43.  
  44.     for (i = 0; i < n && ret == 0; i++) {
  45.         if (!cstart) {
  46.             if (buf[i] == '/' && buf[i+1] == '*')
  47.                 cstart = True;
  48.         } else {
  49.             if (buf[i] == 'X' && buf[i+1] == 'P' && buf[i+2] == 'M')
  50.                 ret = 1;
  51.         }
  52.     }
  53.  
  54.     fclose(fd);
  55.     return ret;
  56. }
  57.  
  58. Image *ReadXPM(char *file)
  59. {
  60.     Display        *dpy = Global.display;
  61.     XImage        *xim, *mim;
  62.     int        x, y, i;
  63.     Image        *image;
  64.     XpmAttributes    attr;
  65.     XColor        *col;
  66.     unsigned char    *ucp, *ump;
  67.     unsigned short    *usp;
  68.     Colormap    cmap = DefaultColormap(dpy, DefaultScreen(dpy));
  69.     int        status;
  70.  
  71.     attr.valuemask = XpmReturnPixels;
  72.     if ((status = XpmReadFileToImage(dpy, file, &xim, &mim, &attr)) != XpmSuccess) {
  73.         switch (status) {
  74.         case XpmColorError:
  75.             RWSetMsg("XPM Color Error");
  76.             break;
  77.         case XpmSuccess:
  78.             RWSetMsg("Success, shouldn't have error & success");
  79.             break;
  80.         case XpmOpenFailed:
  81.             RWSetMsg("XPM Open Failed");
  82.             break;
  83.         case XpmFileInvalid:
  84.             RWSetMsg("File Invalid");
  85.             break;
  86.         case XpmNoMemory:
  87.             RWSetMsg("Not enough memory");
  88.             break;
  89.         case XpmColorFailed:
  90.             RWSetMsg("Unable to allocate color");
  91.             break;
  92.         }
  93.         XpmFreeAttributes(&attr);
  94.         return NULL;
  95.     }
  96.  
  97.     image = ImageNewCmap(attr.width, attr.height, attr.npixels);
  98.     if (mim != NULL) {
  99.         ImageMakeMask(image);
  100.         ump = image->maskData;
  101.     }
  102.  
  103.     ucp = (unsigned char *)image->data;
  104.     usp = (unsigned short *)image->data;
  105.  
  106.     col = (XColor *)XtMalloc(sizeof(XColor) * attr.npixels);
  107.     for (i = 0; i < attr.npixels; i++) {
  108.         col[i].pixel = attr.pixels[i];
  109.         col[i].flags = DoRed|DoGreen|DoBlue;
  110.     }
  111.     XQueryColors(dpy, cmap, col, attr.npixels);
  112.     for (i = 0; i < attr.npixels; i++) 
  113.         ImageSetCmap(image, i, col[i].red >> 8, col[i].green >> 8, col[i].blue >> 8);
  114.     
  115.  
  116.     for (y = 0; y < xim->height; y++) {
  117.         for (x = 0; x < xim->width; x++) {
  118.             Pixel    p;
  119.  
  120.             if (mim != NULL) {
  121.                 Pixel    f = XGetPixel(mim, x, y);
  122.  
  123.                 if (!(*ump++ = (Boolean)f)) {
  124.                     if (attr.npixels > 256) 
  125.                         *usp++ = 0;
  126.                     else
  127.                         *ucp++ = 0;
  128.                     continue;
  129.                 }
  130.             }
  131.  
  132.             p = XGetPixel(xim, x, y);
  133.  
  134.             for (i = 0; i < attr.npixels && col[i].pixel != p; i++) 
  135.                 ;
  136.             
  137.             if (attr.npixels > 256)
  138.                 *usp++ = i;
  139.             else
  140.                 *ucp++ = i;
  141.         }
  142.     }
  143.  
  144.     XtFree((XtPointer)col);
  145.     XDestroyImage(xim);
  146.     XpmFreeAttributes(&attr);
  147.  
  148.     return image;
  149. }
  150.